<?php
/***
* @version $Id: admin_cache.php 336 2007-01-23 08:12:56Z flexiondotorg $
* @copyright (c) 2006 - 2007 Flexion.Org
*            (c) 2004 - 2005 Project Minerva
*            (c) 2001 - 2006 phpBB Group
* @license   http://opensource.org/licenses/gpl-license.php GNU Public License
***/

define('IN_R3BORN', true);

if ( !empty($setmodules) )
{
    $module['General']['Cache'] = basename(__FILE__);
    return;
}

//
// Load default header
//
$root_path = './../';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
require('./pagestart.' . $phpEx);

function purge_template_cache($dir)
{
	global $phpEx;

	$skip_files = array(
		'.',
		'..',
		'.htaccess',
		'index.htm',
		'index.html',
		'index.php',
	);

	$res = @opendir($dir);
	if($res)
	{
		while(($file = readdir($res)) !== false)
		{
			// Match template cache files
			if ( strpos($file, $phpEx) && !in_array($file, $skip_files))
			{
				// CHMOD for write access then Delete file
				@chmod('0777', $dir . '/' . $file);
				@unlink($dir . '/' . $file);

				//print ' [ delete ' . $dir . '/' . $file . ' ] ';

			}
			else
			{
				//print ' [ ignore ' . $dir . '/' . $file . ' ] ';
			}
		}
		closedir($res);
	}
}

if (!isset($_POST['confirm']))
{
	//
	// Set template files
	//
	$template->set_filenames(array(
		'confirm' => 'confirm_body.tpl')
	);

	$template->assign_vars(array(
		'MESSAGE_TITLE' => $lang['Confirm'],
		'MESSAGE_TEXT' => $lang['Confirm_purge_cache'],

		'L_YES' => $lang['Yes'],
		'L_NO' => $lang['No'],

        "S_CONFIRM_ACTION" => append_sid('admin_cache.' . $phpEx),
        //"S_HIDDEN_FIELDS" => $hidden_fields)
		)
    );

    $template->pparse('confirm');
    exit();
}
else
{
    //Purge 'admin' template cache
	purge_template_cache($root_path . $template->cache_root . 'admin');

	//
	//Purge Styles template cache (s)
	//
	$sql = "SELECT template_name FROM " . THEMES_TABLE;

	if (!($result = $db->sql_query($sql)))
	{
		message_die(CRITICAL_ERROR, 'Could not query theme information', '', __LINE__, __FILE__, $sql);
	}

	while ($row = $db->sql_fetchrow($result))
	{
		$dir = $root_path . $template->cache_root . $row['template_name'];
		purge_template_cache($dir);
	}
	$db->sql_freeresult($result);

	$message = $lang['Cache_purged'] . "<br /><br />" . sprintf($lang['Click_return_admin_index'], "<a href=\"" . append_sid("index.$phpEx?pane=right") . "\">", "</a>");

    message_die(GENERAL_MESSAGE, $message);
}

?>